package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.user.Collect;
import com.cskaoyan.bean.user.CollectExample;
import com.cskaoyan.bean.zhao_order_bean.BasePageData;
import com.cskaoyan.mapper.CollectMapper;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.TopicMapper;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description: ${description}
 * @author: cuicui
 **/
@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    CollectMapper collectMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    TopicMapper topicMapper;

    @Override
    public void updateOrdelete(WxCollectBO bo) {
        Integer uid = getUserId();
        //通过user_id、type、value_id查询collect表中是否有此数据
        CollectExample example = new CollectExample();
        CollectExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(uid).andValueIdEqualTo(bo.getValueId()).andTypeEqualTo(bo.getType().byteValue());
        List<Collect> collects = collectMapper.selectByExample(example);

        //如果有->修改deleted
        if(collects.size()!=0){
            Collect record = new Collect();
            record.setUpdateTime(new Date());
            if(collects.get(0).getDeleted()==true){
                record.setDeleted(false);
            }else {
                record.setDeleted(true);
            }
            collectMapper.updateByExampleSelective(record, example);
            return;
        }
        //如果没有->新增
        Collect record = new Collect();
        record.setUserId(uid);
        record.setValueId(bo.getValueId());
        record.setType(bo.getType().byteValue());
        record.setAddTime(new Date());
        record.setUpdateTime(new Date());
        record.setDeleted(false);
        collectMapper.insert(record);
    }

    private Integer getUserId() {
        Subject subject = SecurityUtils.getSubject();
        User primaryPrincipal = (User) subject.getPrincipals().getPrimaryPrincipal();
        return primaryPrincipal.getId();
    }

    @Override
    public BasePageData collectList(Integer type, Integer limit, Integer page) {
       //根据user_id、deleted=false的数据
        Integer uid = getUserId();
        CollectExample example = new CollectExample();
        example.setOrderByClause("update_time desc");
        CollectExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(uid).andDeletedEqualTo(false);
        List<Collect> collects = collectMapper.selectByExample(example);
        //分页
        PageHelper.startPage(page,limit);
        //封装结果返回
        List<CollectListVO> listDTOS = new ArrayList<>();
        for (Collect collect : collects) {
            CollectListVO listDTO = new CollectListVO();
            listDTO.setId(collect.getId());
            listDTO.setType((int)collect.getType());
            listDTO.setValueId(collect.getValueId());
            //通过type、valueId查询商品信息
            if(collect.getType()==1){
                //专题
                Topic topic = topicMapper.selectByPrimaryKey(collect.getValueId());
                listDTO.setBrief(topic.getSubtitle());
                listDTO.setName(topic.getTitle());
                listDTO.setPicUrl(topic.getPicUrl());
                listDTO.setRetailPrice(topic.getPrice().intValue());
            }
            if(collect.getType()==0){
                //商品
                Goods goods = goodsMapper.selectByPrimaryKey(collect.getValueId());
                listDTO.setBrief(goods.getBrief());
                listDTO.setName(goods.getName());
                listDTO.setPicUrl(goods.getPicUrl());
                listDTO.setRetailPrice(goods.getRetailPrice().intValue());
            }
            listDTOS.add(listDTO);
        }
        BasePageData basePageData = BasePageData.ok(listDTOS);
        return basePageData;
    }
}
